﻿@{
    ViewBag.Title = "库存查询";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}@using LeSoft.Application.Code;
<style>
    #gridTable td {
        border-right: 1px solid #ccc;
    }

    .ui-jqgrid .footrow-ltr td {
        background-color: rgb(255, 253, 205);
        border-right: 1px solid rgb(204, 204, 204);
    }
</style>


<script>
    var _organizeid = "@OperatorProvider.Provider.Current().CompanyId", _pstructId = "", _WarehouseType = "", _ProductType = "";
    $(function () {
        InitialPage();
        GetTree1();
        GetTree2();
        GetWarehouse();
    }
     );
    //初始化页面
    function InitialPage() {
        //layout布局
        $('#layout').layout({
            applyDemoStyles: true,
            onresize: function () {
                $(window).resize()
            }
        });
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 181.5);
            }, 200);
            e.stopPropagation();
        });
        $("#PStructTree").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            height: "250px",
            click: function (item) {
                if (_organizeid != item.id) {
                    _organizeid = item.id;
                    $('#btn_Search').trigger("click");
                }
            },
            description: "===选择组织机构==="
        });
        $("#PStructTree").ComboBoxTreeSetValue(_organizeid);




    }
    function GetTree1() {
        var item = {
            //   height: $(window).height(),
            url: "../../ResourceManage/RS_Warehouse/GetTreeJson",
            onnodeclick: function (item) {
                _WarehouseType = item.id;
                GetWarehouse();
                $('#btn_Search').trigger("click");

            }
        };
        //初始化
        $("#itemTree1").treeview(item);
    }
    function GetTree2() {
        var item = {
            url: "../../ResourceManage/RS_Product/GetTypeTreeJson",
            onnodeclick: function (item) {
                _ProductType = item.id;
                $('#btn_Search').trigger("click");
            }
        };
        //初始化
        $("#itemTree2").treeview(item);
    }

    //加载表格
    function GetGrid(data, count) {
        var selectedRowIndex = 0;
        var $gridTable = $('#gridTable');
        var queryJson = {
            organizeid: $("#PStructTree").attr('data-value') == "" ? _organizeid : $("#PStructTree").attr('data-value'),
            condition: $("#queryCondition").find('.dropdown-text').attr('data-value'),
            keyword: $("#txt_Keyword").val(),
            WarehouseType: _WarehouseType,
            ProductType: _ProductType
        };
        $gridTable.jqGrid({
            url: "../../ResourceManage/RS_ProductStock/GetWarehouseProductList",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json",
            height: $(window).height() - 170,
            autowidth: true,
            colModel: getCols(count, data),
            viewrecords: true,
            rowNum: 30,
            rowList: [30, 50, 100],
            pager: "#gridPager",
            sortname: 'Date',
            sortorder: 'desc',
            rownumbers: true,
            gridview: true,
            footerrow: false,
            onSelectRow: function (status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });

        $gridTable.jqGrid('setGroupHeaders', {
            useColSpanStyle: true,
            groupHeaders: GetHeaders(data, count)
        });
        //查询条件
        $("#queryCondition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search").click(function () {
            var queryJson = {
                _organizeid: $("#PStructTree").attr('data-value'),
                condition: $("#queryCondition").find('.dropdown-text').attr('data-value'),
                keyword: $("#txt_Keyword").val(),
                WarehouseType: _WarehouseType,
                ProductType: _ProductType
            };
            $gridTable.jqGrid('setGridParam', {
                url: "../../ResourceManage/RS_ProductStock/GetWarehouseProductList",
                postData: { queryJson: JSON.stringify(queryJson) }
            }).trigger('reloadGrid');
        });

    }

    //获得仓库集合
    function GetWarehouse() {
        //卸载JqGrid
        jQuery("#gridTable").GridUnload();
        $.ajax({
            url: "../../ResourceManage/RS_ProductStock/GetWarehouseData",
            type: "get",
            dataType: "json",
            data: { WarehouseType: _WarehouseType, ProductType: _ProductType },
            success: function (data) {
                var s = data.length;
                GetGrid(data, s);
            }
        })
    }
    //生成分组
    function GetHeaders(data, count) {
        var Headers = [];
        Headers.push({ startColumnName: 'Type', numberOfColumns: 7, titleText: '物品信息' });
        for (var i = 0; i < count; i++) {
            Headers.push({ startColumnName: 'bj' + data[i].Code, numberOfColumns: 6, titleText: data[i].Name });
        }
        return Headers;

    }
    //生成表头
    function getCols(count, data) {
        var cols = [];
        cols.push({ label: "主键", name: "Id", hidden: true });
        cols.push({ label: "类别名称", name: "Type", index: "Type", width: 150, align: "center", sortable: false });
        cols.push({ label: "物品编号", name: "Code", index: "Code", width: 150, align: "center", sortable: false });
        cols.push({ label: "物品名称", name: "Name", index: "Name", width: 150, align: "center", sortable: false });
        cols.push({ label: "规格型号", name: "ModelNo", index: "ModelNo", width: 100, align: "center", sortable: false });
        cols.push({ label: "品牌", name: "Brand", index: "Brand", width: 150, align: "center", sortable: false });
        cols.push({
            label: "计量单位", name: "Unit", index: "Unit", width: 100, align: "center", sortable: false,
            formatter: function (cellvalue, options, rowObject) {
                debugger;
                return top.clientdataItem.CalUnit[cellvalue] == null ? "" : top.clientdataItem.CalUnit[cellvalue];
            }
        });
        cols.push({
            label: "库存数量", name: "Volume", index: "Volume", width: 150, align: "center", sortable: false,
            formatter: function (cellvalue, options, rowObject) {
                if (cellvalue == "" || cellvalue == null) {
                    return "0";
                }
                else {
                    return cellvalue;
                }
            }
        });
        for (var i = 0; i < count; i++) {
            cols.push({ label: "标记", name: "bj" + data[i].Code, hidden: true });
            cols.push({ label: "数量", name: "Num" + data[i].Code, index: "Num", width: 100, align: "center", sortable: false });
            cols.push({ label: "平均单价", name: "AverageUnitPrice" + data[i].Code, index: "AverageUnitPrice", width: 100, align: "center", sortable: false });
            cols.push({ label: "税前单价", name: "TaxUnitPrice" + data[i].Code, index: "TaxUnitPrice", width: 100, align: "center", sortable: false });
            cols.push({ label: "税后单价", name: "UnitPrice" + data[i].Code, index: "UnitPrice", width: 100, align: "center", sortable: false });
            cols.push({ label: "总金额", name: "Price" + data[i].Code, index: "Price", width: 100, align: "center", sortable: false });
        }

        cols.push({ label: "备注", name: "Description", index: "Description", width: 100, align: "center", sortable: false });

        return cols;
    }



    function btn_add() {
        dialogOpen({
            id: "WarehouseProduct",
            title: '添加仓库物品',
            url: '/ResourceManage/RS_ProductStock/Form',
            width: "800px",
            height: "550px",
            callBack: function (iframeId) {
                top.frames["WarehouseProduct"].AcceptClick();
            }
        });
    }//新建仓库物品
    function btn_edit() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "WarehouseProduct",
                title: '修改物品信息',
                url: '/ResourceManage/RS_ProductStock/Form?keyValue=' + keyValue,
                width: "800px",
                height: "550px",
                callBack: function (iframeId) {
                    top.frames["WarehouseProduct"].AcceptClick();
                }
            })

        }
    }//编辑
    function btn_delete() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (keyValue) {
            $.RemoveForm({
                url: '/ResourceManage/RS_ProductStock/RemoveWarehouseProductForm',
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#gridTable").resetSelection();
                    $("#gridTable").trigger("reloadGrid");
                }
            })
        } else {
            dialogMsg('请选择需要删除的数据项！', 0);
        }
    }//删除仓库物品
    function btn_WarehouseInfo() {
        dialogOpen({
            id: "WarehouseInfoIndex",
            title: '仓库资料',
            url: '/ResourceManage/RS_Warehouse/Index',
            width: "1100px",
            height: "800px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            },
            btn: null
        });
    }//仓库资料
    function btn_ProductType() {
        dialogOpen({
            id: "ProductTypeForm",
            title: '物品类型',
            url: '/ResourceManage/RS_Product/TypeIndex',
            width: "1100px",
            height: "800px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            },
            btn: null
        });
    }//物品类型
    function btn_ProductInfo() {
        top.tablist.newTab({
            id: "ProductInfoForm",
            title: '物品资料',
            closed: true,
            icon: "fa fa-cube",
            url: top.contentPath + '/ResourceManage/RS_Product/Index'
        });
    };//物品信息
</script>
<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-west">
        <div class="west-Panel" style=" height:40%; overflow-y: auto; ">
            <div id="itemTree1"></div>
        </div>
        <div class="west-Panel" style="height: 56.5%; overflow-y: auto;">
            <div id="itemTree2"></div>
        </div>
    </div>
    <div class="ui-layout-center">
        <div class="center-Panel">
            <div class="titlePanel">
                <div class="title-search">
                    <table>
                        <tr>
                            <td><div id="PStructTree" style="width:200px;" type="selectTree" class="ui-select "></div></td>
                            <td style="padding-left: 2px;">
                                <div class="btn-group" id="queryCondition">
                                    <a class="btn btn-default dropdown-text" data-toggle="dropdown" data-value="All">全部条件</a>
                                    <a class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                                    <ul class="dropdown-menu">
                                        <li><a data-value="All">全部条件</a></li>
                                        <li><a data-value="Code">编号</a></li>
                                        <li><a data-value="Name">名称</a></li>
                                        <li><a data-value="Brand">品牌</a></li>
                                        <li><a data-value="ModelNo">规格型号</a></li>
                                    </ul>
                                </div>
                            </td>
                            <td style="padding-left: 2px;">
                                <input id="txt_Keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;" />
                            </td>
                            <td style="padding-left: 5px;">
                                <a id="btn_Search" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="toolbar">
                    <div class="btn-group">
                        <a id="lr-replace" class="btn btn-default" onclick="reload(); "><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                        @*<a id="lr-add" class="btn btn-default" onclick="btn_add()"><i class="fa fa-plus"></i>&nbsp;新增</a>
                            <a id="lr-edit" class="btn btn-default" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>&nbsp;编辑</a>
                            <a id="lr-delete" class="btn btn-default" onclick="btn_delete()"><i class="fa fa-trash-o"></i>&nbsp;删除</a>

                            <div class="btn-group">
                                <a id="lr-more" class="btn btn-default" data-toggle="dropdown"><i class="fa fa-reorder"></i>&nbsp;更多操作<span class="caret"></span></a>
                                <ul class="dropdown-menu pull-right">
                                    <li id="lr-type"><a onclick="btn_WarehouseInfo()"><i></i>&nbsp;仓库资料</a></li>
                                    <li id="lr-item"><a onclick="btn_ProductType()"><i></i>&nbsp;物品类别</a></li>
                                    <li id="lr-content"><a onclick="btn_ProductInfo()"><i></i>&nbsp;物品资料</a></li>
                                </ul>

                            </div>*@
                    </div>

                </div>
            </div>

            <div class="gridPanel">
                <table id="gridTable"></table>
                <div id="gridPager"></div>
            </div>
        </div>
    </div>
</div>